package men.adg666.momo;

import android.content.Intent;
import android.graphics.Color;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class WordActivity extends AppCompatActivity {
     String[] EnglishList;
      String[] ChineseList;
     String newChineselist[];
    String newEnglishlist[];
   protected TextView wordshow;
   protected Button submit;
   protected Button advanceSubmit;
   protected EditText inputword;
   List randomtwenty;
   int finalCorrectNumber;
   int finalWrongNumber;
   int finalQuestionNumber;
   String pages;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word);
        submit = findViewById(R.id.submit);
        advanceSubmit =  findViewById(R.id.advanceSubmit);
        inputword = findViewById(R.id.inputword);
        wordshow = findViewById(R.id.wordshow);
        Intent intent = getIntent();
        int a = intent.getIntExtra("pages",0);
        int mode = intent.getIntExtra("mode",0);
        pages = String.valueOf(a);
        getword(pages);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        switch (mode){
            case 1 : AllMode();
                break;
            case 2 :randomtwentyMode();


        }



    }



    public  void getword(final String pages){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://momo.adg666.men/wordlist/" + pages + ".dat")
                            .build();
                    Response response = client.newCall(request).execute();
                    String responsedata = response.body().string();//获取单词库并解析
                    String[] wordlist = responsedata.split("#");//分割字符串
                    EnglishList = new String[wordlist.length];//英文词库
                    ChineseList = new String[wordlist.length];//中文词库
                    for (int i = 0; i < wordlist.length; i++) {
                        String[] newlist = wordlist[i].split("&");
                        ChineseList[i] = newlist[0];
                        EnglishList[i] = newlist[1];
                        Log.d("WordActivity", ChineseList[i]);
                    }
                    //分割字符串，获取内容
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }



    public synchronized void showWord (final String wordtext){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                wordshow.setText(wordtext);
                inputword.setText("");
            }
        });

    }


    public  void  Alljudgement (){
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String input = inputword.getText().toString();
                String show = wordshow.getText().toString();
                final ArrayList arrayList = new ArrayList();//将单词表存入ArrayList便于管理数组
                for (int i=0 ; i<ChineseList.length ; i++){
                    arrayList.add(ChineseList[i]);
                }
                final int number = arrayList.indexOf(show);//获取当前单词组数索引
                String answer = EnglishList[number];

                if (number < ChineseList.length){
                    if(input.equals(answer)){
                        inputword.setText("回答正确");
                        inputword.setTextColor(Color.parseColor("#F9E09398"));

                    }else{
                        inputword.setText("回答错误,应为:"+answer);
                        inputword.setTextColor(Color.parseColor("#F9E09398"));
                    }

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(1000);
                                if (number< ChineseList.length-1){
                                    showWord(ChineseList[number+1]);
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }else{
                    submit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            inputword.setText("答题完成");
                        }
                    });
                }
            }
        });
    }



    public void AdvanceSubmit(){
        advanceSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SubmitData();

            }
        });
    }


    public void SubmitData (){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(WordActivity.this,EndActivity.class );
                intent.putExtra("correctNumber", finalCorrectNumber);
                intent.putExtra("wrongNumber", finalWrongNumber);
                intent.putExtra("questionNumber",finalQuestionNumber);
                startActivity(intent);
                finish();
            }
        }).start();
    }

//得到随机20个数数组
public static List getRandomNumList(int nums,int start,int end){
    //1.创建集合容器对象
    List list = new ArrayList();

    //2.创建Random对象
    Random r = new Random();
    //循环将得到的随机数进行判断，如果随机数不存在于集合中，则将随机数放入集合中，如果存在，则将随机数丢弃不做操作，进行下一次循环，直到集合长度等于nums
    while(list.size() != nums){
        int num = r.nextInt(end-start) + start;
        if(!list.contains(num)){
            list.add(num);
        }
    }

    return list;
}

public void getRandomtwentyList(){
    randomtwenty = getRandomNumList(20,0,ChineseList.length);
    for (int i=0 ; i<randomtwenty.size() ; i++){
        int j = (int) randomtwenty.get(i);
        newEnglishlist[i] = EnglishList[j];
        newChineselist[i] = ChineseList[j];
    }


}

        public void randomtwentyJudgement(){
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String input = inputword.getText().toString();
                String show = wordshow.getText().toString();
                final ArrayList arrayList = new ArrayList();//将单词表存入ArrayList便于管理数组
                for (int i=0 ; i<randomtwenty.size() ; i++){
                    int j = (int) randomtwenty.get(i);
                    arrayList.add(newChineselist[j]);
                }
                final int number = arrayList.indexOf(show);//获取当前单词组数索引
                String answer = EnglishList[number];
                if (number < newChineselist.length){
                    if(input.equals(answer)){
                        inputword.setText("回答正确");
                        inputword.setTextColor(Color.parseColor("#F9E09398"));

                    }else{
                        inputword.setText("回答错误,应为:"+answer);
                        inputword.setTextColor(Color.parseColor("#F9E09398"));
                    }

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(1000);
                                if (number< newChineselist.length-1){
                                    showWord(newChineselist[number+1]);
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }else{
                    submit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            inputword.setText("答题完成");
                        }
                    });
                }
            }
        });

        }




public void AllMode(){
    Alljudgement();
    showWord(ChineseList[0]);
    AdvanceSubmit();
}
public void randomtwentyMode(){
        getRandomtwentyList();
    randomtwentyJudgement();
        showWord(newChineselist[0]);
    AdvanceSubmit();

}

    }




